/* 

    GetEntries.c - Read all selected Files from all sources 

    Does support a command template with "Name,..." as well
    and does use then only the commandline names.

    In case of collecting files from listers it does store
    for each entry the lister and entry pointer too (do not
    forget to free it later...:) )
    
*/

#include <proto/dos.h>
#include <proto/exec.h>

#define _DOPUS_MODULE_DEF
#include <dopus/modules.h>

// strcpy() is needed
#define  SDI_TO_ANSI
#include <sdi_std.h>

#include <exec/memory.h>

typedef struct NodeData
{
       APTR lister;
       APTR entry;
};

extern APTR mempool;

/********************************************************************/
// Prototypes

// the function you should call
Att_List *GetFiles( IPCData *ipc, FuncArgs *fargs, DOpusCallbackInfo *dc );

Att_List *GetSourceEntries( IPCData *ipc, DOpusCallbackInfo *dc );
Att_List *GetArgNames(      long     farg );

/********************************************************************/

Att_List *GetFiles( IPCData *ipc, FuncArgs *fargs, DOpusCallbackInfo *dc )
{
     Att_List *entrylist = NULL;

     if( fargs && fargs->FA_Arguments[0] )
          entrylist = GetArgNames( fargs->FA_Arguments[0] );

     if( IsListEmpty((struct List *) entrylist) )
       {
          Att_RemList( entrylist, NULL );
          entrylist = GetSourceEntries( ipc, dc );
       }

     if( IsListEmpty((struct List *) entrylist) )
       {
          Att_RemList( entrylist, NULL );
          return NULL;
       }

     return entrylist;
}

/********************************************************************/

Att_List *GetSourceEntries( IPCData *ipc, DOpusCallbackInfo *dc )
{
    ULONG num;
    APTR entry, handle;
    char buffer0[256], buffer1[256];
    struct NodeData *nd;
    Att_List *entrylist;
    
    entrylist = Att_NewList( LISTF_POOL );
    
    handle = dc->dc_GetSource( IPCDATA(ipc), buffer0 );
    
    do
      {
         while( (num = dc->dc_ExamineEntry((entry = dc->dc_GetEntry(IPCDATA(ipc))), EE_NAME)) )
           {
              if( (nd=AllocMemH(mempool, sizeof(struct NodeData))) )
                {
                   nd->lister = handle;
                   nd->entry  = entry;
                   AddPart( strcpy(buffer1, buffer0), (STRPTR) num, 256 );
                   Att_NewNode( entrylist, buffer1, (ULONG) nd, NULL );
                }
                
              dc->dc_EndEntry( IPCDATA(ipc), entry, TRUE );
           }
       }
     while( (handle = dc->dc_NextSource(IPCDATA(ipc), buffer0)) );
       
     return entrylist;
}

/********************************************************************/

Att_List *GetArgNames( long farg )
{
     Att_List *entrylist;
     ULONG *argument_names;

     argument_names = (ULONG *) farg;
        
     entrylist = Att_NewList( LISTF_POOL );
    
     do
       {
          Att_NewNode( entrylist, (STRPTR) *(argument_names), NULL, NULL );
       }
     while( *++argument_names );
    
     return entrylist;
}

/********************************************************************/